package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.ServiceException;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class RegisterEventServlet
 */
@WebServlet("/RegisterEventServlet")
public class RegisterEventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterEventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    /**
     * Method invoked when loading "registerevent.jsp"
     */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		
		HttpSession session = request.getSession(true);
		
		PlaceService service2 = new PlaceService();
		List<Place> places = new ArrayList<Place>();
		/**
		 * Get all places
		 */
		places = service2.getAllPlaces();
		
		List<Integer> idPlaces = new ArrayList<Integer>();
		List<String> namePlaces = new ArrayList<String>();
		
		
		for (int i = 0; i < places.size(); i++) {
			idPlaces.add(places.get(i).getId());
			namePlaces.add(places.get(i).getNamePlace());
		}
		
		/**
		 * Storing places for id and name
		 */
		Map<Integer, String> aux = new HashMap<Integer, String>();
		for (int i = 0; i < idPlaces.size(); i++) {
			aux.put(idPlaces.get(i), namePlaces.get(i));
		}
		
		request.setAttribute("placeMap", aux);
		

		/**
		 * Invokes the action "registerevent.jsp"
		 */
		RequestDispatcher dispatcher = request.getRequestDispatcher("registerevent.jsp");
		dispatcher.forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	/**
	 * Method invoked when trying to register an event
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();  
        
		HttpSession session = request.getSession(true);
		
		
		/**
		 * Parameters obtained form
		 */
		String e = request.getParameter("eventname");
		String ed = request.getParameter("eventdescription");
		String p = request.getParameter("places");
		String d1 = request.getParameter("date1");
		String du1 = request.getParameter("duration1");
		String du2 = request.getParameter("duration2");
		String et = request.getParameter("eventype");
		String a = request.getParameter("availabletickets");
		
		
		int aN = Integer.parseInt(a);
		int pN = Integer.parseInt(p);
		
		
		EventService service = new EventService();
	    
		Event event = new Event();
	     
		event.setName(e);
		event.setDescription(ed);
		event.setPlace("");
		event.setDayEvent(d1);
		event.setStartEvent(du1);
		event.setEndEvent(du2);
		event.setEventType(et);
		event.setSeatsAvailable(aN); 
		event.setIdPlace(pN);
		
	    
	    try {
	    	/**
	    	 * Enter an event
	    	 */
	    	service.insertEvent(event);
	    	
	    	/**
			 * Invokes the action "adminvisitor.jsp"
			 */
	    	//request.getRequestDispatcher("adminvisitor.jsp").forward(request, response);
	    	response.sendRedirect("ShowEventsAdminServlet");
	    	
	    	
		} catch (ServiceException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} 
	    out.close(); 
		
		
	 }

}
